using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Deferred.Library
{
    /// <summary>
    /// 
    /// </summary>
    public interface IQuadRenderer
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="v1"></param>
        /// <param name="v2"></param>
        void Render(Vector2 v1, Vector2 v2);
    }

    /// <summary>
    /// 
    /// </summary>
    public class QuadRenderer : GraphicalComponent, IQuadRenderer
    {
        private VertexDeclaration vertexDecl = null;
        private VertexPositionTexture[] verts = null;
        private short[] ib = null;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="graphicsService"></param>
        public QuadRenderer(IGraphicsDeviceService graphicsService)
            : base(graphicsService)
        {
            verts = new VertexPositionTexture[]
                        {
                            new VertexPositionTexture(new Vector3(0,0,1), new Vector2(1,1)),
                            new VertexPositionTexture(new Vector3(0,0,1), new Vector2(0,1)),
                            new VertexPositionTexture(new Vector3(0,0,1), new Vector2(0,0)),
                            new VertexPositionTexture(new Vector3(0,0,1), new Vector2(1,0))
                        };

            ib = new short[] { 0, 1, 2, 2, 3, 0 };
        }

        /// <summary>
        /// 
        /// </summary>
        protected override void LoadContent()
        {
            vertexDecl = new VertexDeclaration(GraphicsDevice, VertexPositionTexture.VertexElements);
        }

        /// <summary>
        /// 
        /// </summary>
        protected override void DestroyContent()
        {
            vertexDecl.Dispose();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="v1"></param>
        /// <param name="v2"></param>
        public void Render(Vector2 v1, Vector2 v2)
        {
            GraphicsDevice.VertexDeclaration = vertexDecl;

            verts[0].Position.X = v2.X;
            verts[0].Position.Y = v1.Y;

            verts[1].Position.X = v1.X;
            verts[1].Position.Y = v1.Y;

            verts[2].Position.X = v1.X;
            verts[2].Position.Y = v2.Y;

            verts[3].Position.X = v2.X;
            verts[3].Position.Y = v2.Y;

            GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, verts, 0, 4, ib, 0, 2);
        }
    }
}
